package fy.hr.dao;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.json.JSONObject;

import com.dayee.wintalent.service.v8.ExternalResumeService;
import com.dayee.wintalent.service.v8.ExternalResumeServicePortType;

import fy.hr.bean.RecruitmentBean;

public class RecruitmentPersonInfoDao {
	private String appid;
	ExternalResumeService mywebService = new ExternalResumeService();
	ExternalResumeServicePortType hs = mywebService.getExternalResumeServiceHttpPort();
	private String applyid;
	private static Log log = LogFactory.getLog(RecruitmentPersonInfoDao.class);
	private RecruitmentBean recruitmentBean;
	private List<String> list1 = new ArrayList<String>();
	private List<String> list2 = new ArrayList<String>();
	private List<String> list3 = new ArrayList<String>();
	private List<String> list4 = new ArrayList<String>();
	private List<String> list5 = new ArrayList<String>();
	private List<String> list6 = new ArrayList<String>();
	private List<String> list7 = new ArrayList<String>();
	private List<String> list8 = new ArrayList<String>();
	private List<String> list9 = new ArrayList<String>();
	private List<String> list10 = new ArrayList<String>();
	private List<String> list11 = new ArrayList<String>();
	private List<String> list12 = new ArrayList<String>();
	private List<String> list13 = new ArrayList<String>();
	private List<String> list14 = new ArrayList<String>();
	private List<String> list15 = new ArrayList<String>();
	private List<String> list16 = new ArrayList<String>();
	private List<String> list17 = new ArrayList<String>();
	private List<String> list18 = new ArrayList<String>();
	private List<String> list19 = new ArrayList<String>();
	private List<String> list20 = new ArrayList<String>();
	private List<String> list21 = new ArrayList<String>();
	private List<String> list22 = new ArrayList<String>();
	private List<String> list23 = new ArrayList<String>();

	//@SuppressWarnings("rawtypes")
	public JSONObject idnumfindPersonInfo(String idnum) throws Exception {
		if (idnum == null||idnum.length()<=0) {
			
			log.info("idnum is null"+idnum.length());
			
		} else {
			try {
				String file = hs.readEntryInformation("<?xml version='1.0' encoding='UTF-8'?>"
						+ "<Condition><corpCode><![CDATA[fuyaogroup]]></corpCode>"
						+ "<userName><![CDATA[fuyaogroup]]></userName>"
						+ "<password><![CDATA[U48w3TAD9PphCcTr]]></password>"
						+ "<currentPage><![CDATA[10]]></currentPage>"
						+ "<rowSize><![CDATA[1]]></rowSize><applyStatus><![CDATA[18]]></applyStatus>"
						+ "<cType><![CDATA[1,4]]></cType>" + "<auditType><vType><![CDATA[1]]></vType>"
						+ "<![CDATA[4]]></auditType>" + "<auditStatus><![CDATA[2]]></auditStatus>"
						+ "<originResumeAttachment><![CDATA[0]]></originResumeAttachment>" + "<idNum>" + idnum
						+ "</idNum>" + "</Condition>");
				System.out.println("~~~~~~~**************?" + file);
				SAXReader reader = new SAXReader();
				org.dom4j.Document doc = reader.read(new ByteArrayInputStream(file.getBytes("UTF-8")));
				org.dom4j.Element root = doc.getRootElement();
				Element a_out = root.element("Resume");
				Attribute g = a_out.attribute("applyId");
				applyid = g.getValue();
				// System.out.println(applyid);
				org.dom4j.Element element;
				org.dom4j.Element element2;
				org.dom4j.Element element3;
				org.dom4j.Element element4;
				JSONObject jo = new JSONObject();
				// "/ResumeList/Resume/ResumeContent/PersonalInformation/";
				RecruitmentBean rb = new RecruitmentBean();
				rb.setIdnum(idnum);
				for (Iterator i = root.elementIterator("Resume"); i.hasNext();) {
					element = (org.dom4j.Element) i.next();
					for (Iterator j = element.elementIterator("ResumeContent"); j.hasNext();) {
						element2 = (org.dom4j.Element) j.next();
						// "/ResumeList/Resume/ResumeContent/EducationBackground/Major")
						for (Iterator k = element2.elementIterator("PersonalInformation"); k.hasNext();) {
							element3 = (org.dom4j.Element) k.next();
							rb.setName(element3.elementText("Name"));
							rb.setGender(element3.elementText("Gender"));
							rb.setAge(element3.elementText("Age"));
							rb.setHometown(element3.elementText("Hometown"));
							rb.setShoolName(element3.elementText("SchoolName"));
							rb.setCliploma(element3.elementText("Diploma"));
							
							rb.setWorkYear(element3.elementText("WorkYear"));
							rb.setCurrentCompany(element3.elementText("CurrentCompany"));
							//ExpectedTreatment
							//rb.setExpectedSalary(element3.elementText("ExpectedSalary"));
							rb.setExpectedSalary(element3.elementText("ExpectedTreatment"));
							rb.setCurrentPosition(element3.elementText("CurrentPosition"));
							rb.setRecruitmentChannels(element3.elementText("RecruitmentChannels"));
							rb.setWorkPlace(element3.elementText("WorkPlace"));
							rb.setMajor(element3.elementText("Major"));
						}
						for (Iterator k1 = element2.elementIterator("CurrentSalaryStructure"); k1.hasNext();) {
							element3 = (org.dom4j.Element) k1.next();
							rb.setBasic(element3.elementText("Basic"));
							rb.setMonthlyBonus(element3.elementText("MonthlyBonus"));
							rb.setMealAllowance(element3.elementText("MealAllowance"));
							rb.setTransportationSubsidy(element3.elementText("TransportationSubsidy"));
							rb.setCellPhoneSubsidy(element3.elementText("CellPhoneSubsidy"));
							rb.setHousingSubsidies(element3.elementText("HousingSubsidies"));
							rb.setYearEndBonus(element3.elementText("YearEndBonus"));
							rb.setOtherSubsidies(element3.elementText("OtherSubsidies"));
							rb.setComprehensiveMonthlyIncome(element3.elementText("ComprehensiveMonthlyIncome"));
							rb.setAnnualCashIncome(element3.elementText("AnnualCashIncome"));
							rb.setWelfare(element3.elementText("Welfare"));
							rb.setOtherBenefits(element3.elementText("OtherBenefits"));
						}
						// "/ResumeList/Resume/InterviewEvaluation/Interviewer/Items/";
						for (Iterator j2 = element.elementIterator("InterviewEvaluation"); j2.hasNext();) {
							element2 = (org.dom4j.Element) j2.next();
							for (Iterator k2 = element2.elementIterator("Interviewer"); k2.hasNext();) {
								element3 = (org.dom4j.Element) k2.next();
								for (Iterator l2 = element3.elementIterator("Items"); l2.hasNext();) {
									element4 = (org.dom4j.Element) l2.next();
									list1.add(element4.elementText("Apperance"));
									list2.add(element4.elementText("Character"));
									list3.add(element4.elementText("Knowledge"));
									list4.add(element4.elementText("Responsibility"));
									list5.add(element4.elementText("Attitude"));
									list6.add(element4.elementText("CommunicationCollaboration"));
									list7.add(element4.elementText("Flexibility"));
									list8.add(element4.elementText("PressureCapacity"));
									list9.add(element4.elementText("SelfRecognition"));
									list10.add(element4.elementText("LogicalThinking"));
									list11.add(element4.elementText("WorkExperience"));
								}
								// "/ResumeList/Resume/InterviewEvaluation/Interviewer/ProfessionalDimensions/";
								for (Iterator l3 = element3.elementIterator("ProfessionalDimensions"); l3.hasNext();) {
									element4 = (org.dom4j.Element) l3.next();
									list12.add(element4.elementText("ProfessionalExpertise"));
									list13.add(element4.elementText("ProfessionalQualifications"));
									list14.add(element4.elementText("ProfessionalExperience"));
									list15.add(element4.elementText("ProfessionalSkills"));
								//	<DevelopmentPotential><![CDATA[4]]></DevelopmentPotential><AdvantageThanInternalCandidates
									list16.add(element4.elementText("DevelopmentPotential"));
									list17.add(element4.elementText("AdvantageThanInternalCandidates"));
								}
								// "/ResumeList/Resume/InterviewEvaluation/Interviewer/ItemsBelowAreAppliedToManagersFactoryDirectorsAndHigher/";
								for (Iterator l4 = element3.elementIterator(
										"ItemsBelowAreAppliedToManagersFactoryDirectorsAndHigher"); l4.hasNext();) {
									element4 = (org.dom4j.Element) l4.next();
									list18.add(element4.elementText("LeadershipSkillsCooperationWithOtherDeparts"));
									list19.add(
											element4.elementText("AbilityOnPlanningOrganizingDivisionAndSupervision"));
									list20.add(element4.elementText("EvelopingTalents"));
								}
								for (Iterator l5 = element3.elementIterator("Zonghepingjia"); l5.hasNext();) {
									element4 = (org.dom4j.Element) l5.next();
									list21.add(element4.elementText("Advantage"));
									list22.add(element4.elementText("Disadvantage"));
									list23.add(element4.elementText("ComprehensiveAssessment"));
								}
							}
						}
					}
					rb.setApplyid(applyid);
					// System.out.println(list1.size());
					if (0 < list1.size()) {
						rb.setApperance(list1.get(0));
						rb.setCharacter(list2.get(0));
						rb.setKnowledge(list3.get(0));
						rb.setResponsibility(list4.get(0));
						rb.setAttitude(list5.get(0));
						rb.setCommunicationCollaboration(list6.get(0));
						rb.setFlexibility(list7.get(0));
						rb.setPressureCapacity(list8.get(0));
						rb.setSelfRecognition(list9.get(0));
						rb.setLogicalThinking(list10.get(0));
						rb.setWorkExperience(list11.get(0));
						rb.setProfessionalExpertise(list12.get(0));
						rb.setProfessionalQualifications(list13.get(0));
						rb.setProfessionalExperience(list14.get(0));
						rb.setProfessionalSkills(list15.get(0));
						rb.setDevelopmentPotential(list16.get(0));
						rb.setAdvantageThanInternalCandidates(list17.get(0));
						rb.setLSSC(list18.get(0));
						rb.setAOPO(list19.get(0));
						rb.setEvelopingTalents(list20.get(0));
						rb.setAdvantage(list21.get(0));
						rb.setDisadvantage(list22.get(0));
						rb.setComprehensiveAssessment(list23.get(0));
						if (list1.size() > 1) {
							rb.setApperance2(list1.get(1));
							rb.setCharacter2(list2.get(1));
							rb.setKnowledge2(list3.get(1));
							rb.setResponsibility2(list4.get(1));
							rb.setAttitude2(list5.get(1));
							rb.setCommunicationCollaboration2(list6.get(1));
							rb.setFlexibility2(list7.get(1));
							rb.setPressureCapacity2(list8.get(1));
							rb.setSelfRecognition2(list9.get(1));
							rb.setLogicalThinking2(list10.get(1));
							rb.setWorkExperience2(list11.get(1));
							rb.setProfessionalExpertise2(list12.get(1));
							rb.setProfessionalQualifications2(list13.get(1));
							rb.setProfessionalExperience2(list14.get(1));
							rb.setProfessionalSkills2(list15.get(1));
							rb.setDevelopmentPotential2(list16.get(1));
							rb.setAdvantageThanInternalCandidates2(list17.get(1));
							rb.setLSSC2(list18.get(1));
							rb.setAOPO2(list19.get(1));
							rb.setEvelopingTalents2(list20.get(1));
							rb.setAdvantage2(list21.get(1));
							rb.setDisadvantage2(list22.get(1));
							rb.setComprehensiveAssessment2(list23.get(1));
						}
					}
					jo.put(rb.getID(), rb.getJSONObject());
				}
				return jo;
			} catch (Exception e) {
				log.error("Recruitment error:", e);
			}
		}

		return null;
	}

	public boolean statusCallback() {
		String temp = this.recruitmentBean.getIdnum();
		try {
			if (temp.equals("") || temp == null) {
				log.info("statusCallback idnum is errorl");
			} else {
				appid = this.recruitmentBean.getApplyid();
				String status = hs.callbackToChangeStatus("<?xml version='1.0' encoding='UTF-8'?>" + "<Condition>"
						+ "<corpCode><![CDATA[fuyaogroup]]></corpCode>" + "<userName><![CDATA[fuyaogroup]]></userName>"
						+ "<password><![CDATA[U48w3TAD9PphCcTr]]></password>" + "<cType><![CDATA[1]]></cType> "
						+ "<applyIds>" + appid + "</applyIds>" + "</Condition>");
				log.info("Recruitment statusCallback:" + status);
				if (status.equals("00")) {
					log.info("Recruitment statuscallback success");
					callbackToChangeStatus();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("Recruitment statuscallback fail");
		}
		return true;
	}

	public boolean callbackToChangeStatus() {
		String temp = this.recruitmentBean.getIdnum();
		try {
			if (temp.equals("") || temp == null) {
				log.info("callback idnum is errorl");
			} else {
				String status = hs.callbackToChangeStatus("<?xml version='1.0' encoding='UTF-8'?>" + "<Condition>"
						+ "	<corpCode><![CDATA[fuyaogroup]]></corpCode>"
						+ "	<userName><![CDATA[fuyaogroup]]></userName>"
						+ "<password><![CDATA[U48w3TAD9PphCcTr]]></password>" + "<applyIds>" + appid + "</applyIds>"
						+ "<auditType><![CDATA[4]]></auditType>" + "<result><![CDATA[0]]></result>"
						+ "<changeType><![CDATA[2]]></changeType>" + "</Condition>");
				log.info("Recruitment callbackToChangeStatus:" + status);

				if (status.equals("00")) {
					log.info("callback success");
					// System.out.println("callback success");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("callback fail");
			// System.out.println("callback fail");
		}
		return true;
	}

	public RecruitmentBean getRecruitmentBean() {
		return recruitmentBean;
	}

	public void setRecruitmentBean(RecruitmentBean recruitmentBean) {
		this.recruitmentBean = recruitmentBean;
	}
}
